package com.netease.xone.itemview;

import java.lang.reflect.Type;
import java.util.List;

import protocol.meta.AppImage;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityImageBrowser;
import com.netease.xone.fragment.FragmentImageBrowser;
import com.netease.xone.widget.GalleryFlow;
import com.netease.xone.widget.LoadingImageView;


public class RenderDetailGallery implements IRenderInfoBase, OnClickListener{
    private static final String TAG = RenderDetailGallery.class.getSimpleName();
    
    private int mWidth;
    private int mHeight;
    
    private View mRoot;
    private GalleryFlow mViewPager;
    private TextView mTitle;

    public RenderDetailGallery(View v) {
        mRoot = v;
        mWidth = v.getContext().getResources().getDisplayMetrics().widthPixels;
        mHeight = v.getContext().getResources().getDimensionPixelSize(R.dimen.detail_size_480);
        
        mViewPager = (GalleryFlow)v.findViewById(R.id.pager);
        mTitle = (TextView)v.findViewById(R.id.title);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        
    }
    
    public void renderView(final Context context, final List<AppImage> list, String title) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
        mRoot.setVisibility(View.GONE);
        mTitle.setVisibility(View.GONE);
        
        if (null != list && list.size() > 0) {
            mRoot.setVisibility(View.VISIBLE);
            mViewPager.setAdapter(new GalleryAdapter(context, list));
            mViewPager.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Gson gson = new Gson();
                    Type collectionType = new TypeToken<List<AppImage>>() {}.getType();
                    String value = gson.toJson(list, collectionType);
                    ActivityImageBrowser.startActivity(context, position, FragmentImageBrowser.TYPE_GALLERY, value);
                }
                
            });
        }
        
        if (!TextUtils.isEmpty(title)) {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }
   
    public static class GalleryAdapter extends BaseAdapter {
        int width; 
        int height;
        Context context;
        List<AppImage> mList;
        
        public GalleryAdapter (Context context, List<AppImage> list) {
            width = context.getResources().getDimensionPixelSize(R.dimen.detail_size_360);
            height = context.getResources().getDimensionPixelSize(R.dimen.detail_size_480);
            this.context = context;
            mList = list;
        }

        @Override
        public int getCount() {
            if (null != mList) {
                return mList.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (null != mList) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LoadingImageView imageview = null;
            imageview = new LoadingImageView(parent.getContext());
            if (null != imageview) {
                imageview.setLoadingImage(mList.get(position).getUrl());
            }
            imageview.setLayoutParams(new GalleryFlow.LayoutParams(width, height));
            return imageview;
        }
        
    }
    
}
